package jdbcutils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtils {
	
	
	private final static  String driver = "com.mysql.jdbc.Driver";
	
	private static String dataBaseName ;
	
	private final static String url = "jdbc:mysql://localhost:3306/student";

	private static String usrname = "root";
	private static String password = "netjava";
	private static Connection con;
	
//	private static Statement sta;
	
//	public JDBCUtils(String dataBaseName,String root,String password){
//		
//		JDBCUtils.dataBaseName = dataBaseName;
//		JDBCUtils.usrname = root;
//		JDBCUtils.password = password;
//		
//	}
	
	static {
		
		try {
			Class.forName(driver);
			con= DriverManager.getConnection(url,"root","netjava");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	public static void insert(String table,String value) throws SQLException{
		
		Statement sta = con.createStatement();
		String sql = "INSERT INTO "+ table + " VALUES('" + value +"')";
		System.out.println(sta.executeUpdate(sql) + " has inserted !");
		sta.close();
	}
	
	
	public static void insert(String sql) throws SQLException{
		
		Statement sta = con.createStatement();
		System.out.println(sta.executeUpdate(sql) + " row(s) has inserted !");
	}
	
	
	public static void delete(String db,String limited) throws SQLException{
		Statement sta = con.createStatement();
		String sql = "DELETE FROM " +  db + " WHERE " + limited;
		System.out.println(sta.executeUpdate(sql) + " row(s) has been delete! " );
		sta.close();

	}
	
	
	public static void delete(String sql) throws SQLException{
		
		Statement sta = con.createStatement();
		
		System.out.println(sta.executeUpdate(sql) + " row(s) has been delete! " );
		
		sta.close();
	}
	
	
	public static void update(String table ,String value, String limited) throws SQLException{
		
		Statement sta = con.createStatement();
		
		String sql = "UPDATE " +  table + " SET " + value + " WHERE " + limited;
		System.out.println( sta.executeUpdate(sql) + " row(s) has been updated! " );
		sta.close();

	}
	
	
	public static void update(String sql) throws SQLException{
		Statement sta = con.createStatement();
		System.out.println(sta.executeUpdate(sql) + " row(s) has been updated! " );
		sta.close();
	}
	
	
	
	public static void query(String what,String table,String limitd) throws SQLException{
		
		Statement sta = con.createStatement();
		
		String sql = "SELECT " + what + " FROM " + table +" WHERE " + limitd;  
		
		ResultSet rs = sta.executeQuery(sql);
		
	    while(rs.next()){
	    	
	    	for(int i=1;i<rs.getMetaData().getColumnCount();i++){
	    		
	    		System.out.print(rs.getString(i)+"\t");
	    		
	    	}
	    	System.out.println();
	    }
	    
	    sta.close();
	}
	
	
	public static void query(String sql) throws SQLException{
		
		Statement sta = con.createStatement();
		
		ResultSet rs = sta.executeQuery(sql);
		
	    while(rs.next()){
	    	
	    	for(int i=1;i<=rs.getMetaData().getColumnCount();i++){
	    		
	    		System.out.print(rs.getString(i)+"\t");
	    		
	    	}
	    	System.out.println();
	    }
	    sta.close();
		
	}
	
	
	
	
	
	

}
